package com.one.controller;

import com.alibaba.fastjson.JSON;
import com.one.annotation.RequestPermission;
import com.one.annotation.RequestUser;
import com.one.bean.ConsigneeInfo;
import com.one.bean.User;
import com.one.config.LoginConfig;
import com.one.constant.RedisPrefixConstant;
import com.one.constant.RequestReturnConstant;
import com.one.service.UserInfoService;
import com.one.util.RedisUtils;
import com.one.util.RequestReturn;
import com.one.util.RequestReturnFactory;
import com.one.vo.UpUserVo;
import com.one.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author ShiWen
 * @date 2022/1/19 9:43
 * @Version 1.0
 * @Description TODO 用户个人信息服务接口
 */
@RestController
@Slf4j
public class UserInfoController {
    @Autowired
    private RedisUtils redisUtils;
    @Autowired
    private LoginConfig loginConfig;
    @Autowired
    private UserInfoService userInfoService;

    /**
     * @description 查询用户信息
     * @author ShiWen
     * @date 2022/1/19 9:45
     */
    @RequestPermission
    @GetMapping("/getUser")
    public RequestReturn<UserVo> queryUser(@RequestUser UserVo userVo, HttpServletRequest request){
        // 获取token
        String token = request.getHeader("token");
        // 根据用户id插曲用户信息
        UserVo userVo1 = userInfoService.selectUser(userVo.getUserId());
        // 将查到的数据同步到redis中
        redisUtils.set(RedisPrefixConstant.LOGIN_TOKEN+token,userVo1);
        return RequestReturnFactory.success(userVo1);
    }

    /**
     * @description 修改密码
     * @author ShiWen
     * @date 2022/1/19 10:11
     */
    @PostMapping("/updatePwd")
    public RequestReturn<String> updatePwd(String phone,String newPwd,String auth){
        // 判断验证码是否存在
        if (!redisUtils.hasKey(loginConfig.getAuthPrefix()+":"+phone)){
            // 不存在验证码，返回验证错误 状态码 5010
            return RequestReturnFactory.fail(RequestReturnConstant.VERIFICATION_FAIL_CODE,RequestReturnConstant.VERIFICATION_FAIL_MSG,"验证码错误");
        }
        // 如果存在 判断验证码是否正确
        if (!StringUtils.pathEquals(redisUtils.get(loginConfig.getAuthPrefix()+":"+phone).toString(),auth)){
            // 验证码错误
            return RequestReturnFactory.fail(RequestReturnConstant.VERIFICATION_FAIL_CODE,RequestReturnConstant.VERIFICATION_FAIL_MSG,"验证码错误");
        }
        if (!userInfoService.updatePwd(phone, newPwd)){
            return RequestReturnFactory.fail("手机号没注册");
        }
        return RequestReturnFactory.success("password ok");
    }
    /**
     * @description 修改个人信息
     * @author ShiWen
     * @date 2022/1/19 13:48
     */
    @RequestPermission
    @PostMapping("/updateUserInfo")
    public RequestReturn<String> updatePwd(@RequestBody UpUserVo user, @RequestHeader String token){
        Object userJson = redisUtils.get(RedisPrefixConstant.LOGIN_TOKEN+token);
        UserVo userVo = JSON.parseObject(userJson.toString(), UserVo.class);
        User newUser = new User();
        BeanUtils.copyProperties(user,newUser);
        newUser.setId(userVo.getUserId());

        if (!userInfoService.updateUser(newUser)){
            return RequestReturnFactory.fail();
        }
        UserVo newUserVo = userInfoService.selectUser(newUser.getId());
        newUserVo.setPhone(userVo.getPhone());
        redisUtils.set(token,newUserVo);
        return RequestReturnFactory.success("修改成功");
    }
    /**
     * @description 添加个人收货信息
     * @author ShiWen
     * @date 2022/1/19 14:26
     */
    @RequestPermission
    @PostMapping("/insertConsignee")
    public RequestReturn<String> insertConsignee(@RequestBody ConsigneeInfo consigneeInfo, @RequestUser UserVo userVo){
        log.info("新增的个人收获信息：{}",consigneeInfo);
        if (consigneeInfo.getUserId()==null){
            consigneeInfo.setUserId(userVo.getUserId());
        }
        consigneeInfo.setIsdefault(false);
        boolean b = userInfoService.insertConsignee(consigneeInfo);
        if (!b){
            return RequestReturnFactory.fail("insert err");
        }
        return RequestReturnFactory.success("添加成功");
    }

    /**
     * @description 删除地址信息
     * @author ShiWen
     * @date 2022/1/19 15:13
     */
    @RequestPermission
    @PostMapping("/delConsignee")
    public RequestReturn<String> delConsigneeById(Integer consigneeId){

        userInfoService.delConsignee(consigneeId);
        return RequestReturnFactory.success("删除成功");
    }

    /**
     * @description 设置地址为默认
     * @author ShiWen
     * @date 2022/1/19 15:24
     */
    @RequestPermission
    @GetMapping("/setDefault")
    public RequestReturn<String> setDefaultAddress(Integer consigneeId,@RequestUser UserVo userVo){

        if (!userInfoService.setDefault(consigneeId, userVo.getUserId())){
            return RequestReturnFactory.fail(RequestReturnConstant.SERVER_SYSTEM_CODE,RequestReturnConstant.SERVER_SYSTEM_MESSAGE);
        }
        return RequestReturnFactory.success("设置成功");
    }

    /**
     * @description 查询用户的所有收货地址
     * @author ShiWen
     * @date 2022/1/19 16:04
     */
    @GetMapping("/queryConsignee")
    public RequestReturn<List<ConsigneeInfo>> queryConsignee(@RequestUser UserVo userVo){

        List<ConsigneeInfo> consigneeInfos = userInfoService.queryAllConsigneeInfo(userVo.getUserId());
        if (consigneeInfos==null){
            return RequestReturnFactory.fail();
        }
        return RequestReturnFactory.success(consigneeInfos);
    }
    /**
     * @description 查询用户默认收货地址
     * @author ShiWen
     * @date 2022/1/19 16:04
     */
    @GetMapping("/defaultConsignee")
    public RequestReturn<ConsigneeInfo> defaultConsignee(@RequestUser UserVo userVo){
        log.info("当前用户的信息：{}",userVo);
        ConsigneeInfo consigneeInfo = userInfoService.queryConsigneeInfoByDefault(userVo.getUserId());
        return RequestReturnFactory.success(consigneeInfo);
    }
}
